package com.guoxin.concurrentprogramming.chapter3.synccontrol;


import java.util.concurrent.locks.ReentrantLock;

/**
 * 公平锁
 * @author Administrator
 */
public class FireLock extends Thread {
    private static final ReentrantLock FILE_LOCK = new ReentrantLock(true);

    @Override
    public void run() {
        while (true) {
            try {
                FILE_LOCK.lock();
                System.out.println(Thread.currentThread().getName() + " get the lock");
            } finally {
                if (FILE_LOCK.isHeldByCurrentThread()) {
                    FILE_LOCK.unlock();
                }
            }
        }
    }

    public static void main(String[] args) {
        FireLock fl = new FireLock();
        new Thread(fl, "T1").start();
        new Thread(fl, "T2").start();
    }
}
